package com.suntoon.framework.exception;

import com.suntoon.framework.common.Constants;
import com.suntoon.framework.common.response.ResultBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.zip.DataFormatException;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年06月25日 13时26分14秒
 * @Description: 全局异常处理
 */
//如果返回的为json数据或其它对象，添加该注解
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * http请求的方法不正确
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Object httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException exception) {
        logger.error("http请求的方法不正确:【" + exception.getMessage() + "】");
        return new ResultBO(Constants.FAIL_CODE, "HTTP请求的方法不正确", exception.getMessage());
    }

    /**
     * 请求参数不全
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultBO missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException exception) {
        logger.error("请求参数不全:【" + exception.getMessage() + "】");
        return new ResultBO(Constants.FAIL_CODE, "请求参数不全", exception.getMessage());
    }

    /**
     * 请求参数类型不正确
     */
    @ExceptionHandler(TypeMismatchException.class)
    public Object typeMismatchExceptionHandler(TypeMismatchException exception) {
        logger.error("请求参数类型不正确:【" + exception.getMessage() + "】");
        return new ResultBO(Constants.FAIL_CODE, "请求参数类型不正确", exception.getMessage());
    }

    /**
     * 数据格式不正确
     */
    @ExceptionHandler(DataFormatException.class)
    public Object dataFormatExceptionHandler(DataFormatException exception) {
        logger.error("数据格式不正确:【" + exception.getMessage() + "】");
        return new ResultBO(Constants.FAIL_CODE, "数据格式不正确", exception.getMessage());
    }

    /**
     * 非法输入
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Object illegalArgumentExceptionHandler(IllegalArgumentException exception) {
        logger.error("非法输入:【" + exception.getMessage() + "】");
        return new ResultBO(Constants.FAIL_CODE, "非法输入", exception.getMessage());
    }

    @ExceptionHandler //处理其他异常
    public Object allExceptionHandler(Exception exception) {
        logger.error("具体错误信息:【" + exception + "】"); //会记录出错的代码行等具体信息
        int count = 0; //只打印15行的错误堆栈
        for (StackTraceElement stackTraceElement : exception.getStackTrace()) {
            logger.error(stackTraceElement.toString());
            if (count++ > 13) break;
        }
        return new ResultBO(Constants.FAIL_CODE, "出错啦", exception.getMessage());
    }
}
